<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>上海九慧信息科技技术有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 1232111 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--#default === slot-scope什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染(类似于table 单元格数据渲染)-->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon icon-class="rjm" />&nbsp;
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="isEdit?'编辑':'添加'" :visible="showDialog" :close-on-press-escape="false" :close-on-click-modal="false" @closed="hClose">
        <!-- 如果用v-if子组件销毁性能不高但是能直接解决添加清空问题 -->
        <add-or-edit :id="curId" ref="depDialogRef" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="showDialog = false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, deleteDepartments } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.depDialogRef.resetForm()
      this.$message.info('已取消操作')
    },
    hSuccess(res) {
      this.showDialog = false
      this.loadDepartments()
      this.$message.info(res.message)
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 异步
      this.$nextTick(() => {
        this.$refs.depDialogRef.loadDepartmentsDetail()
      })
    },
    hDel(id) {
      // 删除部门
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await deleteDepartments(id)
            console.log(res)
            this.loadDepartments()
            this.$message.success('删除成功!')
          } catch (error) {
            console.log(error)
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    handleNodeClick(data) {
      console.log(data)
    },
    async loadDepartments() {
      // 发送请求获取部门数据
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(({ name, code, id, pid }) => ({
          name,
          code,
          id,
          pid
        }))
        console.log('部门信息数据', res.data)
        const company = res.data.depts.shift()
        console.log('第一个公司数据', company)
        console.log('剩下部门信息数据', res.data.depts)
        // res.data.depts.forEach((ele) => {
        //   deleteDepartments(ele.id)
        // })
        this.list = tranListToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
